package com.bluepunch.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bluepunch.entity.Blog;
import com.bluepunch.entity.Category;
import com.bluepunch.entity.Comment;
import com.bluepunch.service.BlogService;
import com.bluepunch.service.CategoryService;
import com.bluepunch.service.CommentService;
import com.bluepunch.vo.QuestionWriteForm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bluepunch
 * @since 2023-10-07
 */
@Controller
//@RequestMapping("/blog")
public class BlogController {
    public static final Long TYPE = 0L;
    @Autowired
    BlogService blogService;
    @Autowired
    CategoryService categoryService;
    @Autowired
    CommentService commentService;

    @GetMapping("/blog")
    public String blogList(Model model) {

        Page<Blog> pageParam = new Page<>(1, 10);
        blogService.page(pageParam, new QueryWrapper<Blog>().orderByDesc("create_time"));
        // 结果
        List<Blog> blogList = pageParam.getRecords();
        model.addAttribute("blogList", blogList);
        model.addAttribute("pageParam", pageParam);

        // 分类信息
        List<Category> categoryList = categoryService.list(null);
        model.addAttribute("categoryList", categoryList);

        return "blog/list";
    }

    @GetMapping("/blog/{page}/{limit}")
    public String blogListPage(
            @PathVariable int page,
            @PathVariable int limit,
            Model model){

        if (page < 1){
            page = 1;
        }
        Page<Blog> pageParam = new Page<>(page, limit);
        blogService.page(pageParam, new QueryWrapper<Blog>().orderByDesc("create_time"));
        // 结果
        List<Blog> blogList = pageParam.getRecords();
        model.addAttribute("blogList", blogList);
        model.addAttribute("pageParam", pageParam);

        // 分类信息
        List<Category> categoryList = categoryService.list(null);
        model.addAttribute("categoryList", categoryList);

        return "blog/list";
    }

    // 写文章
    @GetMapping("/blog/write")
    public String toWrite(Model model) {
        List<Category> categoryList = categoryService.list(null);
        model.addAttribute("categoryList", categoryList);
        return "blog/write";
    }

    @PostMapping("/blog/write")
    public synchronized String write(QuestionWriteForm questionWriteForm) {
        // 构建问题对象

        System.out.println("-------------------------------");
        System.out.println(questionWriteForm);

        Blog blog = new Blog();

        blog.setTitle(questionWriteForm.getTitle());
        blog.setContent(questionWriteForm.getContent());
        blog.setViews(0);

        blog.setAuthorId(questionWriteForm.getAuthorId());
        blog.setAuthorName(questionWriteForm.getAuthorName());
        blog.setAuthorAvatar(questionWriteForm.getAuthorAvatar());

        Category category = categoryService.getById(questionWriteForm.getCategoryId());
        blog.setCategoryId(questionWriteForm.getCategoryId());
        blog.setCategoryName(category.getCategory());

        blogService.save(blog);

        // 重定向到列表页面
        return "redirect:/blog";
    }

    // 阅读文章
    @GetMapping("/blog/read/{bid}")
    public String read(@PathVariable("bid") String bid, Model model) {
        Blog blog = blogService.getOne(new QueryWrapper<Blog>().eq("id", bid));
        // todo: redis缓存. 防止阅读重复
        blog.setViews(blog.getViews() + 1);
        blogService.updateById(blog);
        model.addAttribute("blog", blog);
        // todo： 查询评论
        List<Comment> commentList = commentService.list(
                new QueryWrapper<Comment>().eq("parent_id", bid).orderByDesc("create_time")
                        .eq("type", TYPE));
        model.addAttribute("commentList", commentList);
        return "blog/read";
    }

    // 评论
    @PostMapping("/blog/comment/{bid}")
    public String comment(@PathVariable("bid") Long bid, Comment comment) {
        System.out.println("---------------------------------------------------------------------------");
        System.out.println(comment);
        // 存储评论
        comment.setType(TYPE);
        comment.setParentId(bid);
        comment.setCreateTime(LocalDateTime.now());
        commentService.save(comment);
        // 重定向到列表页面
        return "redirect:/blog/read/" + bid;
    }
}

